package com.es.disped.web.controller.oj.contest;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.es.disped.api.oj.contest.HustContestService;
import com.es.disped.common.annotation.DynamicDataSource;
import com.es.disped.common.model.MsgModel;
import com.es.disped.common.model.PageModel;
import com.es.disped.core.controller.BaseController;
import com.es.disped.dao.oj.model.Contest;
import com.es.disped.dao.oj.model.ContestExample;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author Anson<br>
 *	Copyright by EasyShare 2019<br>
 *  
 *  All right reserved<br>
 *
 *  Created on 2019年7月17日 下午3:32:04<br>
 *  名称：ContestController.java<br>
 *  描述：竞赛及作业管理<br>
 */
@Api("竞赛及作业管理")
@Controller
@RequestMapping("manage/oj/contest")
public class ContestController extends BaseController{

	@Autowired
	HustContestService hustContestService;
	
	@ApiOperation(value="保存竞赛作业",response=MsgModel.class,httpMethod="POST",notes="保存竞赛或保存作业练习")
	@RequiresPermissions(value={"teacher:save","admin:save"},logical=Logical.OR)
	@RequestMapping(value="/save",method=RequestMethod.POST)
	@DynamicDataSource
	public @ResponseBody MsgModel save(@RequestBody @ApiParam(name="contest",value="竞赛及作业对象",required=true) Contest contest)
	{
		int count=0;
		if(Optional.of(contest)
				.filter(title->contest.getTitle()!=null)
				.filter(stime->contest.getStartTime()!=null)
				.filter(etime->contest.getEndTime()!=null)
				.filter(lang->contest.getLangmask()!=null)
				.isPresent())
		{
			contest.setUserId(this.getSessionUser().getPhone());
			count=hustContestService.insertSelective(contest);
		}
		return this.resultMsg(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"竞赛及作业保存成功":"竞赛及作业保存失败",count==1?contest:"");
	}
	
	
	@ApiOperation(value="更新竞赛作业",response=MsgModel.class,httpMethod="POST",notes="更新竞赛或保存作业练习")
	@RequiresPermissions(value={"teacher:update","admin:update"},logical=Logical.OR)
	@RequestMapping(value="/update",method=RequestMethod.POST)
	@DynamicDataSource
	public @ResponseBody MsgModel update(@RequestBody @ApiParam(name="contest",value="竞赛及作业对象",required=true) Contest contest)
	{
		int count=0;
		if(Optional.of(contest)
				.filter(title->contest.getContestId()!=null)
				.isPresent())
		{
			count=hustContestService.updateByPrimaryKeySelective(contest);
		}
		return this.resultMsg(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"竞赛及作业更新成功":"竞赛及作业更新失败",count==1?contest:"");
	}
	
	
	@ApiOperation(value="删除竞赛作业",response=MsgModel.class,httpMethod="POST",notes="删除竞赛或保存作业练习，逻辑删除")
	@RequiresPermissions(value={"teacher:delete","admin:delete"},logical=Logical.OR)
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@DynamicDataSource
	public @ResponseBody MsgModel delete(@RequestBody @ApiParam(name="contest",value="竞赛及作业对象",required=true) Contest contest)
	{
		int count=0;
		if(!this.isNull(contest.getContestId()))
		{
			contest.setDefunct("Y");//设置为删除标记
			ContestExample deleteExample=new ContestExample();
			deleteExample.createCriteria().andContestIdEqualTo(contest.getContestId());
			count=hustContestService.updateByPrimaryKeySelective(contest);
		}
		return this.resultMsg(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"竞赛及作业删除成功":"竞赛及作业删除失败",count==1?contest:"");
	}
	
	@ApiOperation(value="检索竞赛及作业",response=MsgModel.class,httpMethod="POST",notes="检索竞赛/作业")
	@RequestMapping(value="/list",method=RequestMethod.POST)
	@DynamicDataSource
	public @ResponseBody MsgModel list(@RequestBody @ApiParam(name="contest",value="竞赛/作业对象",required=false) Contest contest)
	{
		ContestExample listExample=new ContestExample();
		ContestExample.Criteria criteria=listExample.createCriteria();
		criteria.andDefunctEqualTo("N");
		if(!this.isNull(contest.getContestId()))
		{
			criteria.andContestIdEqualTo(contest.getContestId());
		}
		if(!this.isNull(contest.getTitle()))
		{
			criteria.andTitleLike("%"+contest.getTitle()+"%");
		}
		if(!this.isNull(contest.getStartTime()))
		{
			criteria.andStartTimeGreaterThanOrEqualTo(contest.getStartTime());
		}
		if(!this.isNull(contest.getEndTime()))
		{
			criteria.andEndTimeLessThanOrEqualTo(contest.getEndTime());
		}
		List<Contest> list=hustContestService.selectByExample(listExample);
		return this.resultMsg(!this.isNull(list)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(list)?"竞赛/作业列表加载成功":"竞赛/作业列表加载失败",!this.isNull(list)?list:"");
	}
	
	
	@ApiOperation(value="分页加载竞赛/作业",response=MsgModel.class,httpMethod="POST",notes="分页加载竞赛/作业，分页参数['offset','limit']")
	@RequestMapping(value="/pages",method=RequestMethod.POST)
	@DynamicDataSource
	public @ResponseBody MsgModel pages(@RequestBody @ApiParam(name="paramMap",value="分页参数[offset,limit]",required=true) Map<String, Object> paramMap)
	{
		ContestExample listExample=new ContestExample();
		PageModel<Contest> pageModel=null;
		if(Optional.of(paramMap)
				.filter(offset->paramMap.containsKey("offset")!=false)
				.filter(limit->paramMap.containsKey("limit")!=false)
				.isPresent())
		{
			pageModel=hustContestService.selectByExampleForOffsetPage(listExample,
					Integer.valueOf(paramMap.get("offset").toString()), Integer.valueOf(paramMap.get("limit").toString()));
			
		}
		return this.resultMsg(!this.isNull(pageModel)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(pageModel)?"竞赛/作业加载成功":"竞赛/作业加载失败",!this.isNull(pageModel)?pageModel:"");
	}
}
